# -*- coding=utf8 -*-
from Handlers.InsertItem import InsertItemHandler
from Handlers.FindItem import FindItemHandler
from Handlers.DelItem import DelItemHandler
import os
from models.InitDB import InitDB
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import Test.Upload
from tornado.options import define, options

define("port", default=7001, help="run on the given port", type=int)


class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        greeting = self.get_argument('greeting', 'Hello')
        print(greeting)
        self.render('index.html', name='msc')

class AddHandler(tornado.web.RequestHandler):
    def get(self):
        greeting = self.get_argument('greeting', 'Hello')
        print(greeting)
        self.render('a.html', name='msc')

if __name__ == "__main__":
    import socket
    # addrs = socket.getaddrinfo(socket.gethostname(),None)
    # for item in addrs:
    #     print(item)
    InitDB()
    tornado.options.parse_command_line()
    app = tornado.web.Application(
        handlers=[
            (r"/", IndexHandler),
            (r"/add", AddHandler),
            (r"^/test_up_load$", Test.Upload.Upload),
            (r"^/upitem$", InsertItemHandler),
            (r"^/find", FindItemHandler),
            (r"^/delete", DelItemHandler),

        ],
        static_path=os.path.join(os.path.dirname(__file__), "static"),
        template_path=os.path.join(os.path.dirname(__file__), "templates"),
    )
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port,address='0.0.0.0')
    tornado.ioloop.IOLoop.instance().start()
